const mixin = require('../../mixins');
Component({
  behaviors: [mixin],
  data: {
    isIDE: false,
	promptBgImg: null
  },
  properties: {
    mf_data: Object,
    boxType: String
  },
  attached() {
    this.setData({
      isIDE: wx.getSystemInfoSync().platform == 'devtools'
    })
  },
  didUpdate() {},
  detached() {},
  methods: {
    clickEvent() {
      let {
        href = "", name1 = "", id = "", hrefType = "", label1 = "", miniOutAppId = "", miniOutVersion = "",promptBgImg="https://100000063-1252208446.file.myqcloud.com/images/MF-EB-P3/TC.png",
          trackingParams = ""
      } = this.properties.mf_data;
      //  半屏小程序跳转，半屏方法不能去嵌套在任何方法内调用
      if(hrefType == "miniOutHalf"){
        //  如果存在埋点参数，触发埋点
        if (trackingParams) {
          this.triggerEvent('triggerComponent', {
            data: {
              trackData: trackingParams,
              type: "track"
            }
          }, {
            bubbles: true,
            composed: true
          });
        }
        //  跳转其他半屏小程序
        wx.openEmbeddedMiniProgram({
          appId: miniOutAppId,
          path: '/' + href,
          envVersion: miniOutVersion,
          success(res) {},
          fail(res) {}
        });
      }else{
        let _that = this;
        if (!this.timer) {
          this.timer = setTimeout(() => {
            this.timer = null;
            if (this.properties.boxType == "nonMember" && !href) {
              this.triggerEvent('joinVip', {
                data: ''
              }, {
                bubbles: true,
                composed: true
              });
            }
            //  如果存在埋点参数，触发埋点
            if (trackingParams) {
              this.triggerEvent('triggerComponent', {
                data: {
                  trackData: trackingParams,
                  type: "track"
                }
              }, {
                bubbles: true,
                composed: true
              });
            }
            //hrefType 热区类型 miniIn:内链接/miniOut:外链接/H5:H5/shopCar:购物车/miniOutHalf:其他半屏小程序/C2B:定制类型
            switch (hrefType) {
              case "miniIn":
                if (href.includes('pages/item/item')) {
                  // 这里是给埋点传的数据
                  let itemId = this.extractIdValueFromUrl(href)
                  this.triggerEvent('triggerComponent', {
                    data: {
                      itemId: itemId,
                      type: 'itemClick'
                    }
                  }, {
                    bubbles: true,
                    composed: true
                  });
                }
                const _path = ['pages/item/categoryNew', 'pages/cart/cart', 'pages/info/info','pages/exploration/exploration'];
                if (_path.includes(href)) {
                  wx.redirectTo({
                    url: '/' + href
                  })
                } else {
                  wx.navigateTo({
                    url: '/' + href
                  })
                }
                break;
              case "miniOut":
                wx.navigateToMiniProgram({
                  appId: miniOutAppId,
                  path: '/' + href,
                  envVersion: miniOutVersion
                })
                break;
              case "H5":
                wx.navigateTo({
                  url: `/pages/litha-launch/official/official?src=${encodeURIComponent(JSON.stringify(href))}`
                })
                break;
              case "shopCar":
                //这里是给微信端home发的埋点事件
                this.triggerEvent('triggerComponent', {
                  data: {
                    itemId: label1,
                    type: 'addCartClick'
                  }
                }, {
                  bubbles: true,
                  composed: true
                });

                //这里是给home.vue发的加购事件
                this.triggerEvent('addCart', {
                  data: label1
                }, {
                  bubbles: true,
                  composed: true
                });
                break;
              case "anchorPoint":
                // 锚点定位
                this.triggerEvent('triggerComponent', {
                  data: {
                    itemId: label1,
                    type: 'anchorsClick'
                  }
                }, {
                  bubbles: true,
                  composed: true
                });
                break;
              case "prompt":
                // 若提示
                wx.showToast({
                  title: label1,
                  icon: 'none',
                  duration: 2000
                })
                break;
              case "promptAndCopy":
                // 弱提示+拷贝
                wx.setClipboardData({
                  data: href,
                  success (res) {
                    wx.showToast({
                      title: label1||'网址已复制，请前往浏览器打开查阅相关信息',
                      icon: 'none',
                      duration: 2000
                    })
                  }
                })
                break;
							case "C2B":
								this.triggerEvent('triggerComponent', {
									data: {
										type: 'c2bClick'
									}
								}, {
									bubbles: true,
									composed: true
								});
								break;
              default:
                break;
            }
          }, 100)
        }
      }
    },
    clossToast(){
      this.setData({
        promptBgImg: null ,
      })
    },
    //截取商品id
    extractIdValueFromUrl(url) {
      // 使用正则表达式匹配 id= 后面的数字
      const regex = /id=(\d+)/;
      const match = url.match(regex);
      if (match && match[1]) {
        return match[1]; // 返回匹配到的数字
      } else {
        return null; // 如果未匹配到数字，返回 null
      }
    }
  },
});
